using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MonoTouch.Foundation;
using MonoTouch.UIKit;
using CoachingOrganizer.Logic.Models.Common;
using CoachingOrganizer.iOS.Helpers;
using CoachingOrganizer.iOS.DataSources;

namespace CoachingOrganizer.iOS.Controllers
{
    public partial class PeriodDetailsViewController : UIViewController
    {
        private readonly PeriodModel _model;

        public PeriodDetailsViewController(PeriodModel model)
            : base("PeriodDetailsView", NSBundle.MainBundle)
        {
            _model = model;
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            UIHelper.RegisterCellClass(GoalsView, GoalsDataSource.CellIdentifer);
            GoalsView.Source = new GoalsDataSource(GoalsView, this, _model);
            InitModel();
        }

        private async void InitModel()
        {
            using (System.Threading.CancellationTokenSource cancellation = new System.Threading.CancellationTokenSource())
                await _model.LoadGoalsAsync(cancellation.Token);
        }

        partial void AddNewGoal(NSObject sender)
        {
            _model.AddNewGoal(NewGoalNameView.Text);
        }

        partial void GoBack(NSObject sender)
        {
            DismissViewController(true, null);
        }
    }
}